package api_servers {
	import UIFree.uif_Timer;
	
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.net.URLVariables;

	public class pjf_gapiServer extends api_externalserver {
		private static var _callbacks: Object;
		private static var _cbid: int;
		public function pjf_gapiServer(serverURL: String, a_sroot: Object) {
			var a_vars: Object = a_sroot.stage.loaderInfo.parameters;
			super(a_sroot, serverURL, a_vars["viewer_id"], '', '');
			vars = a_vars;
			_callbacks 	= new Object();
			_cbid		= 0;			
			ExternalInterface.addCallback('gapiReceive', receiver);
		}
		
		static private function receiver (a_cbid : Number, args : Array) : void {
			if ( _callbacks[a_cbid] ) {
				var cb : Function = _callbacks[a_cbid];
				delete _callbacks[a_cbid];
				cb.apply( cb, args );
			}
		}
		
		static public function exec (method : String, callback : Function = null, ...args ) : * {
			if ( _callbacks != null ) {
				_cbid++;
				_callbacks[_cbid] = callback;
				ExternalInterface.call('callApi', method, _cbid, args);
			}
/*			
			return ExternalInterface.call('' +
				'(function(args, cbid){ ' +  
				'if(typeof ' + method + ' != "function"){ ' +
				'	if(cbid) { document.getElementById("'+ flashId+ '").mailruReceive(cbid, ' + method + '); }' +
				'	else { return '+ method+ '; }' +
				'}' +  			
				'if(cbid) {' + 
				'	args.unshift(function(){ ' + 
				'		var argsArray = []; ' +				 
				'		for(var i=0; i<arguments.length; i++){argsArray[i] = arguments[i]}; ' +
				'		document.getElementById("'+ flashId+ '").mailruReceive(cbid, argsArray) ' + 
				'	}); ' + 
				'}; ' + 
				'return '+ method+ '.apply('+ objectName+ ', args) ' + 
				'})', args, cbid);*/
		}

/*
		override protected function srHelper( arr:Array): URLVariables {// sendRequestHelper
			arr.push( {pn: "app_id", pv: app_id } );
			arr.sortOn('pn');
			var url_vars:URLVariables = new URLVariables();
			for( var i:uint = 0; i < arr.length; i++ ) {
				url_vars[ (arr[i].pn) ] = arr[ i ].pv;
			}
			url_vars["sig"] = makeSig( arr );
			return url_vars;
		}
		
		public function oneQuery(method: String, str_params: String='', _handle: Function=null, a_cache: Boolean=false): void {
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: vars.modelName + ';' + method + ';' + str_params}
			];
			
			jsrHelper(params, _handle, method + MD5.encrypt(str_params), a_cache);
		}
*/		
		
		public function userIn(_handle: *): void {
			pjf_gapiServer.exec('userIn', _handle);
		}
		
		override public function getProfiles(uids: String, _handle: *, fields: String='', queryID:String=''): void {
		}
		
		public function getAlbums(uid: String, _handle: *, queryID:String=''): void {
			pjf_gapiServer.exec('getAlbums', _handle, uid);
		}
		
		public function getPhotos(uid: String, aid: String, _handle: *, queryID:String=''): void {
			pjf_gapiServer.exec('getPhotos', _handle, uid, aid);
		}
		
		override public function getFriends(uid: String, handler_:*, queryID:String='getFriends'): void {
		}
		
		public function getAllFriends(uid: String, handler_:*, queryID:String='getAllFriends'): void {
		}
		
		public function openPayDialog(sms_price: Number, other_price: Number, 
									  openFunc: Function, service_id: int = 2, 
									  service_name: String = 'Пополнение баланса'): void {
		}
	}
}